﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dao;
using dto;
using common;

namespace GTVAccountingSystem.view.form
{
    public partial class frmChangePassword : Form
    {
        UsersDAO usersDAO = new UsersDAO();
        frmMain main;
        int index;
        private String oldPassText = "";
        private String newPassText = "";
        private String newConfirmPassText = "";
        private String error = "";
        public frmChangePassword(frmMain main, int index)
        {
            this.index = index;
            this.main = main;
            InitializeComponent();
            setLanguage();
        }
        /* Author : Jenny
        * Date : 10/4/2012
        * function setLanguage
        * set language
        * no return
        */
        public void setLanguage()
        {
            lblOldPass.Text = Constant.RM.GetString("lblOldPass", Constant.CULTURE) + " : ";
            lblNewPass.Text = Constant.RM.GetString("lblNewPass", Constant.CULTURE) + " : ";
            lblConfirmPass.Text = Constant.RM.GetString("lblConfirmNewPass", Constant.CULTURE) + " : ";
            btnSave.Text = Constant.TAB + Constant.RM.GetString("btnSave", Constant.CULTURE);
            btnCancel.Text = Constant.TAB + Constant.RM.GetString("btnCancel", Constant.CULTURE);
            this.Text = Constant.RM.GetString("frmChangePassword", Constant.CULTURE);
        }
        /* Author : Jenny
         * Date : 4/4/2012
         * function validateData
         * no return
         */ 
        public void validateData()
        {
            oldPassText = txtOldPassword.Text.Trim();
            newPassText = txtNewPassword.Text.Trim();
            newConfirmPassText = txtConfirmPass.Text.Trim();
            error = "";
            if (oldPassText.Equals(""))
            {
                txtOldPassword.BackColor = Color.MistyRose;
                error += "Input Old password\n";
            }
            if (newPassText.Equals(""))
            {
                txtNewPassword.BackColor = Color.MistyRose;
                error += "Input New password\n";
            }
            if (newConfirmPassText.Equals(""))
            {
                txtConfirmPass.BackColor = Color.MistyRose;
                error += "Input Confirm new password\n";
            }
            else if(!newConfirmPassText.Equals(newPassText))
            {
                txtConfirmPass.BackColor = Color.MistyRose;
                error += "The new confirm password is not match the new password";
            }
        }
        /* Author : Jenny
         * Date : 4/4/2012
         * function btnSave_Click
         * save the new password after validate 
         * no return 
         */
        private void btnSave_Click(object sender, EventArgs e)
        {
            validateData();
            if (error.Equals(""))
            {
                String oldPass = Constant.USER_LOGIN.Password;
                if (!oldPass.Equals(oldPassText))
                {
                    MessageBox.Show("The old password is wrong, please reinput", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (newPassText.Equals(oldPassText))
                {
                    MessageBox.Show("Password must difference with the old pass", "Eror", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (usersDAO.changePassword(newPassText, Constant.DR_ACTIONS_VARIABLE["varUpdate"].ToString(), "password"))
                    {
                        Application.Restart();
                    }
                }
            }
            else
            {
                MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /* Author : Jenny
         * Date : 4/4/2012
         * function disableErrorLable
         * hide the error lable when start
         * no return 
         */
        public void disableErrorLable()
        {
            txtOldPassword.BackColor = SystemColors.Window;
            txtNewPassword.BackColor = SystemColors.Window;
            txtConfirmPass.BackColor = SystemColors.Window;
        }
        /* Author : Jenny
         * Date : 4/4/2012
         * function txtNewPassword_TextChanged
         * call checkPassword function when text in password change 
         * no return 
         */
        private void txtNewPassword_TextChanged(object sender, EventArgs e)
        {
            if (txtNewPassword.Text.Length > 8)
            {
                String score = usersDAO.checkPassword(txtNewPassword.Text);
                if (score.Equals("1"))
                    lblNewPass.Text = "Password : ( length 8 - 30 ) WEAK";
                else if (score.Equals("2"))
                    lblNewPass.Text = "Password : ( length 8 - 30 ) MEDIUM";
                else
                    lblNewPass.Text = "Password : ( length 8 - 30 ) STRONG";
            }
        }

        private void frmChangePassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            main.listOpen.Remove(index);
            //main.Enabled = true;
        }
    }
}
